package AssetDepreciation

import (
	"ToolLibray/jwtx"
	"context"
	"property_api/internal/svc"
	"property_api/internal/types"
	"property_api/utils/result"
	"property_service/property_service"

	"github.com/zeromicro/go-zero/core/logx"
)

type PagesLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewPagesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PagesLogic {
	return &PagesLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// Pages 查询折旧信息的分页逻辑
func (l *PagesLogic) Pages(req *types.AssetDepreciationPageReq) (resp *types.PageRes, err error) {
	// 设置租户ID，从上下文中获取
	tenantID := jwtx.GetUserTenantId64(l.ctx)

	// 构建查询条件对象
	where := &property_service.AssetDepreciation{
		TenantID:                tenantID,                    // 租户ID
		ID:                      req.ID,                      // 记录ID
		AssetID:                 req.AssetID,                 // 资产ID（关联资产表）
		AssetCode:               req.AssetCode,               // 资产编码
		AssetName:               req.AssetName,               // 资产名称
		AssetCategory:           req.AssetCategory,           // 资产分类
		AssetCategoryID:         req.AssetCategoryID,         // 资产分类ID
		CardNumber:              req.CardNumber,              // 卡片编号
		DepreciationPeriod:      req.DepreciationPeriod,      // 折旧期间
		DataIdentifier:          req.DataIdentifier,          // 数据标识
		Warehouse:               req.Warehouse,               // 库房
		UsingDept:               req.UsingDept,               // 使用科室
		UsingDeptID:             req.UsingDeptID,             // 使用科室ID
		OriginalValue:           req.OriginalValue,           // 资产原值
		AccumulatedDepreciation: req.AccumulatedDepreciation, // 累计折旧
		DepreciationMethodID:    req.DepreciationMethodID,    // 折旧方法ID
		DepreciationMethod:      req.DepreciationMethod,      // 折旧方法
		PurchaseDate:            req.PurchaseDate,            // 购置日期
		UsefulLife:              req.UsefulLife,              // 使用年限
		ResidualValue:           req.ResidualValue,           // 残值
		StatusID:                req.StatusID,                // 状态ID（关联状态表）
		StatusName:              req.StatusName,              // 状态名称
		Remarks:                 req.Remarks,                 // 备注
		DepreciationStatus:      req.DepreciationStatus,      // 折旧状态
		StockID:                 req.StockID,                 // 仓库ID
		Depreciation:            req.Depreciation,            //月折旧额
		DepreciationRate:        req.DepreciationRate,        //月折旧率
		DepreciationRateYear:    req.DepreciationRateYear,    //年折旧率
		Residual:                req.Residual,                //残值率
		AssetIDs:                req.AssetDepreciationID,     //资产id列表
	}

	// 调用服务层方法，查询折旧信息记录的分页数据
	// 服务层方法 AssetDepreciationPage 会处理实际的数据库查询操作
	data, err := l.svcCtx.AssetService.AssetDepreciationPage(l.ctx, &property_service.AssetDepreciationPageReq{
		Page:  req.Page,  // 当前页码
		Limit: req.Limit, // 每页条数
		Where: where,     // 查询条件
	})

	// 返回分页数据的响应
	return result.SucPage(data, types.AssetDepreciation{}).PageEd()
}
